<style lang="less">
  @import '../styles/common.less';
  @import '../tables/components/table.less';
</style>

<template>
  <div class="div-relative">
    <Row :gutter="10">
      <Card>
        <div class="table-head" v-show="selectionArr.length == 0">
          <Button type="primary" class="Button" @click="add()">添加</Button>
        </div>
        <div class="table-head" v-show="selectionArr.length > 0">
          <div class="left-nums">选中 {{selectionArr.length}} 条 / 共 {{data.length}} 条</div>
          <div class="left-btn">
            <Button type="error" class="Button" @click="removes()">删除</Button>
          </div>
          <div class="left-btn">
            <Button type="warning" class="Button" @click="updateFun()"
                    :disabled="selectionArr.length == 1 ? false : true">修改
            </Button>
          </div>
          <Button type="text" class="Button" @click="hideNums()">取消</Button>
        </div>
        <Row class="margin-top-10">
          <Table stripe border :columns="columns" :data="data" @on-row-click="onRowData"
                 @on-selection-change="selectionChange" ref="selectionTable"></Table>
        </Row>
        <div style="margin: 10px;overflow: hidden;height: 36px;">
          <div style="float: right;">
            <Page :total="total" :current="page" @on-change="changePage" show-elevator></Page>
          </div>
        </div>
      </Card>
      <!--详情-->
      <Modal v-model="showCurrentTableData" :width="600" :mask-closable="false" :closable="false"
             class-name="vertical-center-modal" class="Modal-Detail-div">
        <h3 slot="header" style="color:#2D8CF0" v-text="dataDetail.version + '详情'">详情</h3>
        <Form label-position="top">
          <FormItem label="时间" v-if="dataDetail.create_time">
            <div class="dataDetail-div" v-text="dataDetail.create_time"></div>
          </FormItem>
          <FormItem label="版本" v-if="dataDetail.version">
            <div class="dataDetail-div" v-text="dataDetail.version"></div>
          </FormItem>
          <FormItem label="更新日志" v-if="dataDetail.content">
            <div class="dataDetail-div word-wrap" v-html="dataDetail.content"></div>
          </FormItem>
          <FormItem label="安装包" v-if="dataDetail.url">
            <div class="dataDetail-div">
              <span style="cursor: pointer; color: #2b85e4;" v-if="dataDetail.url != ''"
                    @click="Download(dataDetail.url)">下载安装包</span>
            </div>
          </FormItem>
        </Form>
        <div slot="footer">
          <Button type="text" @click="asyncOK">取消</Button>
          <Button type="warning" @click="update(dataDetail)">修改</Button>
        </div>
      </Modal>
      <!--编辑-->
      <Modal v-model="addCurrentTableData" :width="600" :mask-closable="false" :closable="false"
             class-name="vertical-center-modal" class="Modal-Detail-div">
        <h3 slot="header" style="color:#2D8CF0">编辑</h3>
        <Form label-position="top">
          <FormItem label="版本">
            <Input v-model="addData.version" placeholder="版本"></Input>
          </FormItem>
          <FormItem label="更新日志">
            <Input v-model="addData.content" type="textarea" class="textarea_resize"
                   :autosize="{minRows: 3}" placeholder="内容"></Input>
          </FormItem>
          <FormItem label="安装包">
            <div class="dataDetail-div chooseUser accessory">
              <Upload
                :paste="true"
                type="drag"
                action=""
                :before-upload="handleUpload">
                <div style="padding: 20px 0">
                  <Icon type="ios-cloud-upload" size="52" style="color: #3399ff"></Icon>
                  <p id="upload-p">点击上传，或者拖拽过来</p>
                </div>
              </Upload>
            </div>
          </FormItem>
        </Form>
        <div slot="footer">
          <Button type="text" @click="hideAddModal">取消</Button>
          <Button type="primary" :loading="saveLoading" @click="save()">确定</Button>
        </div>
      </Modal>
    </Row>
    <spin-page :showSpin="showSpin"></spin-page>
  </div>
</template>

<script>
  import Cookies from 'js-cookie';
  import Util from '../../libs/util';
  import Utils from '../../libs/mod.utils';
  import String from '../../libs/mod.string';
  import Modal from '../../libs/mod.Modal';
  import spinPage from '../components/spin-page.vue';
  export default {
    components: {
      spinPage
    },
    data () {
      return {
        total: 0,
        page: 1,
        size: 10,
        order_by: [],
        id: '',
        searchConName: '',
        searchConTel: '',
        showCurrentTableData: false,
        loading: true,
        showCurrentColumns: [],
        editInlineAndCellData: [],
        columns: [
          {
            type: 'selection',
            width: 60,
            align: 'center'
          },
          {
            key: 'version',
            title: '版本',
            align: 'center',
          },
          {
            key: 'create_time',
            title: '时间',
            align: 'center',
          },
        ],
        data: [],
        dataDetail: {},
        initTable: [],
        selectionArr: [],
        format: Util.constant.format,
        addCurrentTableData: false,
        saveLoading: false,
        ajaxType: 'add',
        addData: {
          platform: '',
          version: '',
          content: '',
          file: '',
          url: '',
        },
        platformArr: [
          {
            id: 'android',
            name: 'Android',
          },
          {
            id: 'ios',
            name: 'IOS',
          }
        ],
        showSpin: false,//加载中
      };
    },
    mounted () {
      var _this = this;
      this.getData();

      // 消息中心跳转过来的
      var from_data = sessionStorage.getItem('from_data');
      if (from_data && from_data != '') {
        var data = Utils.toJson(from_data);
        _this.show(data);
        sessionStorage.removeItem('from_data');
      }
    },
    methods: {
      // 取消列表选中
      hideNums: function () {
        var _this = this;
        this.$refs.selectionTable.selectAll(false);
        _this.selectionArr = [];
      },
      updateFun: function () {
        var _this = this;
        var selectionArr = _this.selectionArr;
        if (selectionArr.length == 0) {
          _this.$Message.destroy();
          _this.$Message.error('请选择要修改的数据!');
          return false;
        }
        if (selectionArr.length > 1) {
          _this.$Message.destroy();
          _this.$Message.error('一次只能修改一条数据!');
          return false;
        }
        var data = selectionArr[0];
        _this.update(data);
      },
      // 排序
      onSortChange: function (data) {
        var _this = this;
        var column = data.column;
        var key = data.key;
        switch (key) {
          case 'permission' :
            key = 'permission';
            break;
        }
        var order = data.order;
        if (order == 'asc' || order == 'desc') {
          var orderObj = {
            field: key,
            sort: order,
          };
          _this.order_by = [orderObj];
          _this.getData();
        } else {
          _this.order_by = [];
          _this.getData();
        }
      },
      getData () {
        var _this = this;
        var data = {
          uid: Util.toJson(localStorage.getItem('userInfo')).id,
          page: _this.page,
          size: _this.size,
          order_by: _this.order_by,
        };
        var params = {
          url: '/version/list',
          type: Util.method.post,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        _this.showSpin = true;
        function _successFun(res) {
          _this.showSpin = false;
          if (res.result == 0) {
            var _data = res.data || [];
            _this.total = res.rows;
            _this.data = _data;
            _this.hideNums();
          } else {
            _this.$Message.destroy();
            _this.$Message.error('查询信息失败!');
          }
        }
      },
      handleSearch () {
        var _this = this;
        _this.page = 1;
        var searchConName = _this.searchConName;
      },
      changePage (page) {
        var _this = this;
        console.log('page', page);
        _this.page = page;
        _this.getData();
      },
      onRowData: function (data, index) {
        var _this = this;
        _this.show(data);
      },
      show: function (data) {
        var _this = this;
        var id = data.id;
        _this.id = id;
        data.content = data.content.replace(/<br>/g, '');
        _this.dataDetail = data;
        _this.showCurrentTableData = true;
      },
      asyncOK: function () {
        var _this = this;
        _this.showCurrentTableData = false;
      },
      // 选中文件
      handleUpload: function (file) {
        var _this = this;
        _this.UploadFile(file);
        return false;
      },
      // 上传文件
      UploadFile: function (file) {
        var _this = this;
        var name = file.name;
        var arr = name.split('.');
        var type = arr[arr.length - 1];
        if (!(type == 'apk')) {
          _this.$Message.destroy();
          _this.$Message.error('请上传apk安装包!');
          return false;
        }
        var formData = new FormData();
        formData.append("file", file);
        var params = {
          url: '/file/addFile',
          type: Util.method.post,
          data: formData,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var data = res.data;
            var id = data.id;
            var name = data.name;
            var url = data.url;
            _this.addData.file = id;
            _this.addData.url = url;
            $('#upload-p').html('上传成功');
          } else {
            _this.addData.file = '';
            $('#upload-p').html('点击上传，或者拖拽过来');
            _this.$Message.destroy();
            _this.$Message.error('上传失败!');
          }
        }
      },
      // 列表多选 选中数据
      selectionChange: function (selection) {
        var _this = this;
        _this.selectionArr = selection;
      },
      // 隐藏编辑页面
      hideAddModal: function () {
        var _this = this;
        _this.addCurrentTableData = false;
      },
      // 显示添加界面
      add: function () {
        var _this = this;
        _this.ajaxType = 'add';
        _this.addData.platform = '';
        _this.addData.version = '';
        _this.addData.content = '';
        _this.addData.file = '';
        $('#upload-p').html('点击上传，或者拖拽过来');
        _this.addCurrentTableData = true;
      },
      // 保存数据
      save: function () {
        var _this = this;
        var platform = _this.addData.platform;
        var version = _this.addData.version;
        var content = _this.addData.content;
        var file = _this.addData.file;
        if (!(version && version != '')) {
          _this.$Message.destroy();
          _this.$Message.error('请填写版本!');
          return false;
        }
        if (!(content && content != '')) {
          _this.$Message.destroy();
          _this.$Message.error('请填写更新日志!');
          return false;
        }
        if (!(file && file != '')) {
          _this.$Message.destroy();
          _this.$Message.error('请上传安装包!');
          return false;
        }
        var data = {
          id: _this.id,
          version: version,
          content: content,
          file: file,
        };
        var url = '/version/add';
        if (_this.ajaxType == 'update') {
          url = '/version/update';
        }
        var params = {
          url: url,
          type: Util.method.post,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        _this.saveLoading = true;
        function _successFun(res) {
          _this.saveLoading = false;
          if (res.result == 0) {
            _this.page = 1;
            _this.getData();
            _this.addCurrentTableData = false;
            _this.addData.platform = '';
            _this.addData.version = '';
            _this.addData.content = '';
            _this.addData.file = '';
            $('#upload-p').html('点击上传，或者拖拽过来');
          } else {
            _this.$Message.destroy();
            _this.$Message.error('保存信息失败!');
          }
        }

      },
      // 显示修改界面
      update: function (data, index) {
        var _this = this;
        _this.ajaxType = 'update';
        var id = data.id;
        _this.id = id;

        _this.addData.version = data.version;
        _this.addData.content = data.content.replace(/<br>/g, '');
        _this.addData.file = data.file;
        _this.addData.url = data.url;
        _this.addCurrentTableData = true;
        _this.showCurrentTableData = false;
      },
      // 删除多个
      removes: function () {
        var _this = this;
        var selectionArr = _this.selectionArr;
        if (selectionArr.length == 0) {
          _this.$Message.destroy();
          _this.$Message.error('请选择要删除的数据!');
          return false;
        }
        var selectionIds = [];
        for (var k in selectionArr) {
          if (!selectionArr[k]) {
            continue;
          }
          var id = selectionArr[k].id;
          selectionIds.push(id);
        }
        Modal.confirm(_this, {content: '确定删除选中的信息？'}, function () {
          var data = {
            ids: selectionIds,
          };
          var params = {
            url: '/version/del',
            type: Util.method.post,
            data: data,
            success: _successFun
          };
          Util.ajax(_this, params);
          function _successFun(res) {
            if (res.result == 0) {
              _this.page = 1;
              _this.getData();
            } else {
              _this.$Message.destroy();
              _this.$Message.error('删除信息失败!');
            }
          }
        }, function () {
        });
      },
      // 下载
      Download: function (url) {
        var _this = this;
        if (url) {
          Utils.DownloadFile(url, 'APP.apk');
        }
      },
    }
  };
</script>
